package com.control.qct.businesssteward.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.model.GoodsDetailModel;
import com.control.qct.businesssteward.utils.EditTextUtils;
import com.control.qct.businesssteward.utils.GlideUtil;
import com.yanzhenjie.album.Action;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumFile;
import com.yanzhenjie.album.api.widget.Widget;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 个人信息资料
 */
public class PersonageInformationActivity extends BaseActivity {
    @BindView(R.id.civ_head_picture)
    CircleImageView mCivHeadPicture;//头像
    @BindView(R.id.et_name)
    EditText mEtName;//姓名
    @BindView(R.id.tv_job_number)
    TextView mTvJobNumber;//工号
    @BindView(R.id.tv_phone)
    TextView mTvPhone;//手机
    @BindView(R.id.tv_role)
    TextView mTvRole;//角色
    private boolean isEdit;
    private Intent mIntent;
    private String mName;

    public PersonageInformationActivity() {
        super(R.layout.activity_personage_information);
    }

    @Override
    protected void initView() {
        setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("个人信息");
        mName = mEtName.getText().toString();
        mIntent = new Intent();
        mCivHeadPicture.setClickable(false);
        GlideUtil.loadImage(PersonageInformationActivity.this, mSPUtils.getString("head_picture"), mCivHeadPicture);
        EditTextUtils.setIsEdit(mEtName, isEdit);
        setRightIcon("编辑", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isEdit) {
                    mCivHeadPicture.setClickable(false);
                    isEdit = false;
                    setRightIcon("编辑");
                    if (!mEtName.getText().toString().equals(mName)) {
                        mIntent.putExtra("name", mEtName.getText().toString());
                        setResult(RESULT_OK, mIntent);
                    }
                    finish();
                } else {
                    mCivHeadPicture.setClickable(true);
                    isEdit = true;
                    setRightIcon("保存");
                }
                EditTextUtils.setIsEdit(mEtName, isEdit);
            }
        });
    }


    @OnClick(R.id.civ_head_picture)
    public void onViewClicked() {
        Album.image(this) // 选择图片。
                .singleChoice()//单选模式，多选模式为：multipleChoice()。
                .widget(Widget.newDarkBuilder(this)//自定义UI
                        .statusBarColor(Color.parseColor("#05C1AB"))
                        .toolBarColor(Color.parseColor("#05C1AB"))
                        .mediaItemCheckSelector(Color.WHITE, Color.parseColor("#05C1AB"))
                        .bucketItemCheckSelector(Color.RED, Color.YELLOW)
                        .build()).camera(true)// 是否显示相机.
                .columnCount(3)//页面列表的列数。
                .onResult(new Action<ArrayList<AlbumFile>>() {
                    @Override
                    public void onAction(int requestCode, @NonNull ArrayList<AlbumFile> result) {
                        String thumbPath = result.get(0).getThumbPath();
                        mSPUtils.put("head_picture", thumbPath);
                        GlideUtil.loadImage(PersonageInformationActivity.this, thumbPath, mCivHeadPicture);
                        mIntent.putExtra("head_picture", thumbPath);
                        setResult(RESULT_OK, mIntent);
                    }
                })
                .start();
    }
}
